package entity;

import lombok.Getter;
import lombok.Setter;
import org.w3c.dom.events.MouseEvent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;

@Getter
@Setter
@SuppressWarnings("unused")
public abstract class MainJFrame extends JFrame {
    private JMenuBar jMenuBarMain;
    private JMenu functionMenu;
    private JMenu helpMenu;
    private JMenu aboutMenu;

    public MainJFrame() {
        initJMenuBar();

        setFocusable(true);
        requestFocus();

        setSize(400, 400);
        setLocationRelativeTo(null);

        setLayout(null);
        setAlwaysOnTop(true);

        setJMenuBar(jMenuBarMain);

        setFont(new Font("JetBrain Momo", Font.PLAIN, 16));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        KeyboardFocusManager.getCurrentKeyboardFocusManager()
                .addKeyEventDispatcher(e -> {
                    int code = e.getKeyCode();
                    if (code == KeyEvent.VK_ESCAPE) {
                        System.exit(0);
                        return true;
                    }
                    return false;
                });

    }


    private void initJMenuBar() {
        jMenuBarMain = new JMenuBar();
        functionMenu = new JMenu("功能");
        helpMenu = new JMenu("帮助");
        aboutMenu = new JMenu("关于");
        addAll(jMenuBarMain, functionMenu, helpMenu, aboutMenu);

        updateMenuBar();

        JMenuItem exitItem = new JMenuItem("退出游戏");
        exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
        exitItem.addActionListener(_ -> System.exit(0));
        addAll(functionMenu, exitItem);

        JMenuItem toLearnItem = getToLearnItem();
        addAll(helpMenu, toLearnItem);

        JMenuItem stateItem = new JMenuItem("声明");
        stateItem.addActionListener(_ -> {
            JOptionPane.showMessageDialog(this,
                    "本游戏仅为个人学习时所写，屎山代码是正常的。", "声明",
                    JOptionPane.INFORMATION_MESSAGE);
        });
        addAll(aboutMenu, stateItem);
    }

    private JMenuItem getToLearnItem() {
        JMenuItem toLearnItem = new JMenuItem("查看教程");
        toLearnItem.addActionListener(_ -> {
            JLabel messageLabel = new JLabel(
                    "<html><body><p>宫格游戏拼图技巧</p>" +
                            "<p><a href='https://b23.tv/0PFBn87'>https://b23.tv/0PFBn87</a></p></body></html>"
            );
            messageLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            messageLabel.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent evt) {
                    try {
                        // 使用系统默认浏览器打开链接
                        Desktop.getDesktop().browse(java.net.URI.create("https://b23.tv/0PFBn87"));
                    } catch (Exception e) {
                        JOptionPane.showMessageDialog(MainJFrame.this,
                                "无法打开链接: " + e.getMessage(), "错误",
                                JOptionPane.ERROR_MESSAGE);
                    }
                }
            });
            JOptionPane.showMessageDialog(this, messageLabel, "查看教程", JOptionPane.INFORMATION_MESSAGE);
        });
        return toLearnItem;
    }

    public static void addAll(JMenu menu, JMenuItem... menuItems) {
        for (JMenuItem jMenuItem : menuItems) {
            menu.add(jMenuItem);
        }
    }

    public static void addAll(JMenuBar menuBar, JMenu... menus) {
        for (JMenu menu : menus) {
            menuBar.add(menu);
        }
    }

    public static void addAll(JPanel panel, JComponent... components) {
        for (JComponent component : components) {
            panel.add(component);
        }
    }

    public static void addAll(JFrame frame, JComponent... components) {
        for (JComponent component : components) {
            frame.add(component);
        }
    }

    public static JButton createButton(String text, int width, int height, boolean borderVisible, ActionListener listener) {
        JButton button = new JButton(text);
        button.setSize(width, height);
        button.setBorderPainted(borderVisible);
        button.setPreferredSize(new Dimension(width, height));
        button.addActionListener(listener);
        button.setDefaultCapable(false);
        return button;
    }

    public static JLabel createLabel(String text, int width, int height) {
        JLabel label = new JLabel(text);
        label.setSize(width, height);
        label.setPreferredSize(new Dimension(width, height));
        label.setFont(new Font("JetBrain Momo", Font.PLAIN, 20));
        return label;
    }

    public static void TextFieldLayout(JPanel panel, GridBagConstraints gbc, JTextField textField, String text, int y, int width, int height) {
        JLabel label = createLabel(text, width, height);
        textField.setPreferredSize(new Dimension(width, height));
        gbc.gridx = 0;
        gbc.gridy = y;
        panel.add(label, gbc);
        gbc.gridx = 1;
        panel.add(textField, gbc);
    }

    /**
     * 选择性添加菜单栏
     */
    public void updateMenuBar() {

    }
    public abstract void initComponents();
}
